$(() => {

	const $btn = $('.table.table-striped.table-bordered.table-hover.mp20 tbody')

	// 页面渲染
	getCommen()
	// 事件委托
	delegate('.btnAccept')
	delegate('.btnReject')
	delegate('.btnDelete')















	function getCommen(curr) {
		const page = curr ? curr : ''
		$.ajax({
			url: `/admin/comment/search`,
			data: {
				page
			},
			success: res => {
				console.log(res)
				if (res.code !== 200) {
					layer.msg(res.msg, {
						time: 1500
					})
					return
				}
				const count = res.data.totalPage,
					limit = res.data.data.length

				// 获取分页
				layui.use('laypage', () => {
					const laypage = layui.laypage
					laypage.render({
						elem: 'pager',
						limit: limit, //每页的数量
						count: count, //总页数
						curr: page,
						jump: (options, first) => {
							// console.log(options.curr)
							// 如果是第一次加载，退出
							if (first) return
							// 后续点击换页需要递归
							getCommen(options.curr)
						}
					})
				})

				// 渲染页面
				$btn.empty().append(template(
					'commen', {
						data: res.data.data
					}))
			}
		})
	}

	// ajax操作
	function change(url, id) {
		$.ajax({
			type: "post",
			url,
			data: {
				id
			},
			success: res => {
				console.log(res)
				layer.msg(res.msg, {
					time: 1500
				}, getCommen)
			}
		})

	}

	// 事件委托
	function delegate(ele) {
		$btn.on('click', ele, function() {
			let url = '/admin/comment/'
			// console.log($(this).data('id'))
			if (ele === '.btnDelete') {
				url += 'delete'
				layer.confirm('确认删除？', {
					icon: 3,
					title: '删除提示'
				}, index => {
					// 点击确认执行
					change(url, $(this).data('id'))
					// 关闭图标
					layer.close(index)
				})
				return
			}
			url += ele === '.btnAccept' ? 'pass' : 'reject'
			change(url, $(this).data('id'))
		})
	}
})
